/*
 * $RCSfile: Processor_47_SendAction.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-16  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.i366.processor;

import java.util.Map;

import org.apache.log4j.Logger;

import com.i366.Constant;
import com.i366.cache.Cache;
import com.i366.room.model.RoomPersion;
import com.i366.room.model.impl.Room;
import com.i366.service.RoomService;
import com.work.client.util.I366ClientPickUtil;
import com.work.comm.util.I366PickUtil;
import com.work.engine.protocal.request.Request;
import com.work.engine.server.handler.Handler;

/**
 * 发送玩家操作
 * <p>Title: Processor_47_SendAction</p> 
 * <p>Description: </p> 
 * <p>Copyright: Copyright (c) 2006</p> 
 * @author shenxing.ruan
 * @version 1.0
 */
public class Processor_47_SendAction extends Handler {

	private Logger logger = Logger.getLogger(Processor_47_SendAction.class);
	public byte[] handleRequest() {
		logger.debug("Processor_47_SendAction userId is : " + this.getGameRequest().getUserId());
		//=======================相关验证=================
		Request request = getGameRequest();
		RoomService.updateUser(request);
		int[][] int2 = {{60,I366ClientPickUtil.TYPE_INT_1},
				{132,I366ClientPickUtil.TYPE_INT_4},
				{131,I366ClientPickUtil.TYPE_INT_4},
				{133,I366ClientPickUtil.TYPE_INT_4}
		};
		Map<Integer, Object> map = I366ClientPickUtil.pickAll(request.getBt(), int2);
		int action = (Integer)map.get(60);
		int roomId = (Integer)map.get(132);
		int roomPath = (Integer)map.get(131);
		int anteNumber = (Integer)map.get(133);
		try {
			Room room = Cache.getRoom(roomId, roomPath);
			RoomPersion roomPersion = room.getRoomPersions()[room.getCurrentNumber()];
			//不是当前玩家操作
			if (roomPersion.getType() != 3 || roomPersion.getUserId() != request.getUserId()) {
				return pusUser(1);
			}
			//下注筹码少于最大筹码
			if ((action == 2 || action == 3) && (roomPersion.getBetChouma() + anteNumber) < room.getMaxChouma()) {
				return pusUser(1);
			}
			//筹码少于最大筹码不能让牌
			if (action == 5 && roomPersion.getBetChouma() < room.getMaxChouma()) {
				return pusUser(1);
			}
			//下注筹码数理论 不能小于剩余筹码数目
			if (roomPersion.getNowcounma() < anteNumber) {
				return pusUser(1);
			}
			//当前玩家相关状态不对 比如已经超时两次
			if (roomPersion.getStatus() != 0 && roomPersion.getStatus() != 1 && roomPersion.getStatus() != 2 && roomPersion.getStatus() != -1) {
				return pusUser(1);
			}
			switch (action) {
				case 2://跟注
					if (anteNumber < 1) {
						return pusUser(1);
					}
					if (anteNumber == roomPersion.getNowcounma()) {
						room.getRoomService().quanxia(roomPersion, anteNumber);
					}else {
						room.getRoomService().genzhu(roomPersion, anteNumber);
					}
					break;
				case 3://加注
					if (anteNumber < 1) {
						return pusUser(1);
					}
					if (anteNumber == roomPersion.getNowcounma()) {
						room.getRoomService().quanxia(roomPersion, anteNumber);
					}else {
						room.getRoomService().jiazhu(roomPersion, anteNumber);
					}
					break;
				case 4://全下
					if (anteNumber < 1) {
						return pusUser(1);
					}
					room.getRoomService().quanxia(roomPersion, anteNumber);
					break;
				case 5://让牌
					room.getRoomService().rangpai(roomPersion);
					break;
				case 6://弃牌
					room.getRoomService().qipai(roomPersion);
					break;
			}
			return pusUser(0);
		} catch (Exception e) {
			return pusUser(1);
		}
	}

	public byte[] pusUser(int status) {
		Object[][] objs2 = {
				{60,status,I366ClientPickUtil.TYPE_INT_1}
		};
		byte[] bytes = I366PickUtil.packAll(objs2, Constant.REQ_GAME_SEND_ACTION);
		return bytes;
	}
	public Object makeObject() throws Exception {
		return new Processor_47_SendAction();
	}

}

